package com.welike.shibo.utils;

import com.alibaba.fastjson.JSONObject;
import com.welike.shibo.enums.ResultEnum;
import com.welike.shibo.exception.JsonResultException;

/**
 * @author fangyu
 * @version v1.0.0
 * @since 2019/11/22 2:00 下午
 */
public class CommonUtil {

    /**
     * 验证是否含有全部必填字段
     *
     * @param requiredColumns 必填的参数字段名称 逗号隔开 比如"userId,name,telephone"
     */
    public static void hasAllRequired(final JSONObject jsonObject, String requiredColumns) {
        if (!StringTools.isNullOrEmpty(requiredColumns)) {
            //验证字段非空
            String[] columns = requiredColumns.split(",");
            String missCol = "";
            for (String column : columns) {
                Object val = jsonObject.get(column.trim());
                if (StringTools.isNullOrEmpty(val)) {
                    missCol += column + "  ";
                }
            }
            if (!StringTools.isNullOrEmpty(missCol)) {
                throw new JsonResultException(ResultUtils.error(ResultEnum.E_90003));
            }
        }
    }
}
